import axios from "axios";
import qs from "qs"; //qs.stringify()	转换成查询字符串,qs.parse() 转换成json对象

import router from "../router";
import { Toast } from "vant";

// axios 配置
// 请求地址
// axios.defaults.baseURL = "https://us.aistudies.cc/platform-api";
// axios.defaults.baseURL = "http://localhost:8081";
axios.defaults.baseURL = "https://us.harvesthgbc.com/platform-api/";
// 请求超时时间
axios.defaults.timeout = 36000;

axios.defaults.withCredentials = false;
// 请求头的设置
axios.defaults.headers = {
  "Content-Type": "application/json;charset=UTF-8",
  "Content-Security-Policy":
    "script-src 'self'; object-src 'none';style-src cdn.example.org third-party.org; child-src https:",
};
// 发送请求前
axios.interceptors.request.use(
  (config) => {
    // console.log(config)
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截
axios.interceptors.response.use(
  (response) => {
    // console.log(response.config)
    if (
      response.config.url == "/H5/sendSmsCode" ||
      response.config.url == "/H5/login"
    ) {
      return response;
    } else {
      if (
        response.data.errcode == 101 ||
        response.data.msg == "token解析失败"
      ) {
        localStorage.removeItem("PolicyToken");
        router.push("/");
      }
      if (response.data.msg) {
        Toast(response.data.msg);
      }
      return response;
    }
  },
  (error) => {
    if (error && error.response) {
      switch (error.response.status) {
        case 401:
          router.push("/");
          break;
        case 500:
          // Toast(error.response.data)
          break;
      }
    }
    return Promise.reject(error.response); // 返回接口返回的错误信息
  }
);

export function Get(url, param, headers = "application/json;charset=UTF-8") {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        headers: {
          token: localStorage.getItem("PolicyToken"),
          "Content-Type": headers || "application/json;charset=UTF-8",
        },
        params: param,
      })
      .then(
        (response) => {
          // console.log('response     '+    response)
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
}

export function Post(url, params) {
  // console.log(localStorage.getItem('PolicyToken'))
  return new Promise((resolve, reject) => {
    axios
      .post(url, params, {
        headers: {
          token: localStorage.getItem("PolicyToken"),
        },
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
          console.log(err);
        }
      )
      .catch((error) => {
        reject(error);
        console.log(error);
      });
  });
}

export function Put(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .put(url, qs.stringify(params), {
        headers: {
          "Content-Type":
            "application/x-www-form-urlencoded;text/html;charset=utf-8",
        },
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
}

export function PutRequest(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .put(url, params, {
        headers: {
          "Content-Type": "application/json;text/html;charset=utf-8",
        },
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
}

export function Delete(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .delete(url, {
        data: qs.stringify(params),
        headers: {
          "Content-Type":
            "application/x-www-form-urlencoded;text/html;charset=utf-8",
        },
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
}
